home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / prospero / PRM / src / testprog / sortio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  4.6 KB  |  208 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 by the University of Southern California
  3.  *
  4.  * For copying and distribution information, please see the files
  5.  * <prm-copyr.h>.
  6.  *
  7.  * Written by srao 9/92
  8.  *
  9.  *
  10.  */
  11.  
  12. #include <prm-copyr.h>
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <sys/time.h>
  18. #include <fcntl.h>
  19. #include <sys/param.h>
  20.  
  21. #include <comm.h>
  22.  
  23. #ifdef MACH386
  24. #  include <sys/file.h>
  25. #else
  26. #  ifdef HPUX
  27. #    define srandom srand
  28. #    define random rand
  29. #  endif
  30. #  include <unistd.h>
  31. #endif
  32.  
  33.          /* Seed for random number */
  34.  
  35.           static long state1[32]  =  {       3,       0x9a319039,
  36.           0x32d9c024,  0x9b663182,  0x5da1f342,       0x7449e56b,
  37.           0xbeb1dbb0,  0xab5c5918,  0x946554fd,       0x8c2e680f,
  38.           0xeb3d799f,  0xb11ee0b7,  0x2d476b86,       0xda672e2a,
  39.           0x1588ca88,  0xe489735d,  0x904f35f7,       0xd7158fd6,
  40.           0x6fa6f051,  0x616e6b96,  0xac94efdc,       0xde3b81e0,
  41.           0xdf0a6fb5,  0xf103bc02,  0x48f340fb,       0x36413f93,
  42.           0xc622c298,  0xf5a42ab8,  0x8a88d77b,       0xf5ad9d0e,
  43.           0x8999220b, 0x27fb47b9      };
  44.  
  45. char *progname, filename[MAXPATHLEN];
  46.  
  47. float *A;
  48. int nelems;
  49.  
  50. struct message {
  51.   int start_index, nitems;
  52.   char dflname[MAXPATHLEN];
  53. } msg;
  54.  
  55.  
  56. /* THIS IS THE INITIALIZATION PROCEDURE WRITTEN BY APPLICATION PROGRAMMER */
  57.  
  58. TIO_init_procedure()
  59. {
  60.   int i, tmp, length, npernode, numnodes, start_index, ntasks;
  61.   u_long jnum, ntmp1;
  62.   FILE *dfd;
  63.   
  64.   do {
  65.     printf("Enter the size of the array: "); 
  66.     scanf("%d", &nelems);
  67.     if (nelems > 32767)
  68.       printf("Number too large.\n");
  69.   } while (nelems > 32767);
  70.  
  71.   ntasks = numtasks();
  72.   A = (float *)calloc(nelems + 1, sizeof(float));
  73.  
  74.   /* Calculate the number of items each task will get to sort initially */
  75.   npernode = floor((double)nelems/(double)ntasks+0.5);
  76.   
  77.   printf("Name of data file (without suffix): ");
  78.   scanf("%s", filename);
  79.   if (filename[0] != '/') {  
  80.     getwd(msg.dflname);
  81.     strcat(msg.dflname, "/");
  82.     strcat(msg.dflname, filename);
  83.   }
  84.   if ( rndgen(nelems, msg.dflname) == -1) {
  85.     fprintf(stderr, "Could not generate random number array.\n");
  86.     return -1;
  87.   }
  88.  
  89.   for(i=1; i<=ntasks; i++) {  /* send filename, start_index of array and
  90.                  length of subarray to sort to each task */
  91.  
  92.     length = (i<ntasks)? npernode: nelems-(ntasks-1)*npernode;
  93.  
  94.     msg.start_index = (i-1)*npernode;
  95.     msg.nitems = length;
  96.  
  97.     if (vsend(i, 1, 10, &msg, sizeof(struct message)) == SUCCESS)
  98.       printf("sent to task %d\n", i);
  99.     else {
  100.       printf("send error\n");
  101.       return -1;
  102.     }
  103.   }
  104.   return 0;
  105. }
  106.  
  107.  
  108. /* get output - Get a message from task 1 containing the name of output
  109.    data file. Read the data and print it to an ascii file */
  110.  
  111. TIO_procedure(int sndr_taskid, int msg_tag, char *msg_data, int msg_len)
  112. {
  113.   int ofd, i;
  114.   FILE  *od;
  115.   char *outflname;
  116.  
  117.   outflname = msg_data;
  118.   if( (ofd = open(outflname, O_RDONLY, 0)) < 0) {
  119.     fprintf(stderr, "could not open %s:", outflname);
  120.     sleep(3);
  121.     perror("");
  122.     exit(1);
  123.   }
  124.   
  125.   /* Read sorted data file */
  126.   if( read(ofd, A, sizeof(float)*nelems) == 0) {
  127.     fprintf(stderr, "Error reading sorted data file!\n");
  128.     close(ofd);
  129.     return;
  130.   }
  131.   close(ofd);
  132.  
  133.   unlink(msg.dflname);
  134.   printf("\n Sort complete. Input data file=%s.in, sorted data file=%s.out\n",
  135.      filename, filename);
  136.   strcat(filename, ".out");
  137.   od = fopen(filename, "w");
  138.   for (i=0; i< nelems; i++)
  139.     fprintf(od, "%f\n", A[i]);
  140.   fclose(od);
  141.   unlink(outflname);  /* Remove the output data file */
  142.  
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /* Generate an array of random floating point numbers */
  149.  
  150. rndgen(int n, char *datafile)
  151. {
  152.   int i, j, k, r1, r2;
  153.   FILE  *fp1, *fp2;
  154.   char datafile1[100];
  155.  
  156.   initrand(20);
  157.  
  158.   fprintf(stderr, "generating data ...");
  159.   fflush(stderr);
  160.   sprintf (datafile1, "%s.in", datafile); /* Open the input file for writing */
  161.   fp1 = fopen(datafile1, "w");
  162.   if (fp1 == NULL) {
  163.     fprintf(stderr, "Couldn't open %s for write\n", datafile);  
  164.     return -1;
  165.   }
  166.   
  167.   for (i = 0; i< n; i++) {
  168.     
  169.     if ( random()&01)
  170.       A[i]= (float) (random()%1000000)/1000-500;
  171.     else
  172.       A[i] = (float) (random()%1000)-500;
  173.  
  174.     fprintf(fp1, "%f\n", A[i]);
  175.   }
  176.   fclose(fp1);
  177.  
  178.   fp2 = fopen(datafile, "w");   /* The input file to all tasks */
  179.   if (fp2 == NULL) {
  180.     fprintf(stderr, "Couldn't open %s for write\n", datafile);  
  181.     return -1;
  182.   }
  183.   
  184.   fwrite(A, sizeof(float), n, fp2);
  185.   fclose(fp2);
  186.   fprintf(stderr, "done\n");
  187.   return 0;
  188. }
  189.  
  190. initrand(num)
  191. int num;
  192.  
  193. {      
  194.   unsigned seed;      int n;
  195.   struct timeval tx;
  196.   struct timezone tzx;
  197.  
  198.   gettimeofday(&tx,&tzx);
  199.  
  200.   seed =tx.tv_sec%(num*100);       n  =  128;
  201. #if defined(HPUX)
  202.   srand(seed);
  203. #else
  204.   initstate(seed,state1, n);
  205.   setstate(state1);  
  206. #endif
  207. }
  208.